const app = getApp(),
  {
    $host,
    $resHost
  } = app.globalData,
  shareImg = `${$resHost}/img/app/share.jpg`;
let shareUrl = {};
module.exports = {
  onLoad(param) {
    if (param.webHash && ~param.webHash.indexOf('store/list/') || param.webHash && ~param.webHash.indexOf('cases/list/') || param.webHash && ~param.webHash.indexOf('article/list/')) {
      xhs.hideShareMenu();
    }
    shareUrl = param;
    /**
     * url user employee https
     * https 是完整链接 不需要解析
     */
    if (param.scene) {
      app.$getData('/user/qrcode/qrcode-get-page', {
        sceneId: param.scene,
        path: 'pages/WebView/webView'
      }).then(data => {
        if (data.errCode == 0) {
          if (data.fullPath) {
            xhs.redirectTo({
              url: '/' + data.fullPath
            });
          } else {
            console.log('二维码识别错误');
          }
        }
      });
      return false;
    }
    let {
        url = '',
        webHash = '',
        https = '',
        query = ''
      } = param,
      //获取authWechatId
      {
        authWechatId = ''
      } = app.getPathOptions();
    query = decodeURIComponent(param.query ? `?${param.query}` : '');

    //兼容停车场带参数跳转
    let getUrl = xhs.getStorageSync('parkBind');
    if (getUrl && getUrl != '') {
      https = getUrl;
      xhs.removeStorageSync('parkBind');
    }
    https = decodeURIComponent(https);
    if (authWechatId) {
      //如果是其他小程序跳转过来 就添加 authWechatId
      query = query ? `${query}&authWechatId=${authWechatId}` : `?authWechatId=${authWechatId}`;
    }
    app.login().then((data = {}) => {
      let authcode = data.authcode || this.data.authcode || xhs.getStorageSync('authcode'),
        queryAuthcode = authcode ? `?authcode=${authcode}` : '',
        viewUrl = `${$host}/${url}${queryAuthcode}#/${webHash}${query}`;
      if (url === 'https') {
        viewUrl = https;
      }
      this.setData({
        authcode: authcode,
        url: '',
        viewUrl: viewUrl
      });
    });
  },
  clear() {
    this.setData({
      message: {}
    });
  },
  onHide() {
    this.clear();
  },
  onUnload() {
    this.clear();
  },
  onMessage(e) {
    let {
        data
      } = e.detail,
      key,
      info,
      len = data.length;
    if (len) {
      key = Object.keys(data[len - 1])[0];
      info = data[len - 1][key];
      if (key !== 'shareInfo') {
        xhs.setStorageSync(`${key}`, info);
      } else {
        info.imageUrl = info.imgUrl || info.imageUrl;
        delete info.imgUrl;
        this.setData({
          message: {
            [key]: info
          }
        });
      }
      e.detail.data.length = e.detail.data.length - 1;
      //wx.setStorageSync(`${key}`, data[len - 1][key]);
    }
  },
  onShareAppMessage(options) {
    let shareInfo = this.data.message.shareInfo,
      webUrl = options.webViewUrl,
      url = '',
      webHash = '',
      query = '';
    if (shareInfo) {
      //获取web页面分享信息

      webUrl = shareInfo.link || shareInfo.path;
      delete shareInfo.link;
      if (~webUrl.indexOf('/pages/')) {
        //链接为小程序链接
        shareInfo.path = webUrl;
      } else {
        webUrl = webUrl.replace($host, '');
        url = webUrl.replace(/^[/]?(\w*)[?/].*/, '$1');
        webUrl = webUrl.replace(url, '').replace(/^[?/]{1,2}/, '').replace('#/', '&webHash=').replace(/.*webHash=/, '');
        webHash = webUrl.replace(/\?.*/, '');
        query = encodeURIComponent(webUrl.replace(webHash, '').replace('?', ''));
        shareInfo.path = `/pages/WebView/webView?url=${url}&webHash=${webHash}&query=${query}`;
      }
      // this.setData({
      //   message: {}
      // })
    } else if (~webUrl.indexOf('mp.weixin.qq.com')) {
      shareInfo = {
        path: ''
      };
      shareInfo.path = `/pages/WebView/webView?url=${shareUrl.url}&https=${shareUrl.https}`;
    } else if (this.data.twoForward && this.data.twoForward.path != '') {
      //二次webview转发
      shareInfo = this.data.twoForward;
    } else {
      let curRole = options.webViewUrl.replace($host, '').replace(/^[/]?(\w*)[?/].*/, '$1');
      shareInfo = {
        imgUrl: shareImg
      };
      if (~webUrl.indexOf('https://') || ~webUrl.indexOf('http://') || ~webUrl.indexOf('wwww.')) {
        //不做处理    
      } else if (curRole === 'user') {
        shareInfo.path = '/pages/TabBar/Home/home';
        shareInfo.title = '数字门店';
      } else {
        shareInfo.path = '/pages/TabBar/Home/home';
        let {
          independentOrgId,
          entityName
        } = xhs.getStorageSync('employeeInfo');
        url = 'user';
        webHash = `indexPage/1/${independentOrgId}`;
        shareInfo.title = `${entityName}数字门店`;
        shareInfo.path = `/pages/WebView/webView?url=${url}&webHash=${webHash}`;
      }
    }
    shareInfo.success = () => {
      if (shareInfo.hasOwnProperty('transpond')) {
        app.$getData('/user/material/transpond', shareInfo.transpond);
      }
    };
    this.setData({
      twoForward: shareInfo
    });
    return shareInfo;
  }
};